package zgrjb.snbq.teach_platform.controller;

import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import io.jsonwebtoken.Claims;
import zgrjb.snbq.teach_platform.util.JwtUtil;
import zgrjb.snbq.teach_platform.dto.model.User;
import jakarta.servlet.http.HttpServletRequest;
import zgrjb.snbq.teach_platform.util.JsonData;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/test")
@Slf4j
@CrossOrigin(origins = "*")
public class TestController {

    @GetMapping("/hello")
    public JsonData hello() {
        log.info("测试接口被调用");
        return JsonData.buildSuccess("Hello from TeachPlatform Backend!");
    }

    @GetMapping("/paths")
    public JsonData testPaths() {
        Map<String, String> paths = new HashMap<>();
        paths.put("用户登录", "/api/v1/user/login");
        paths.put("用户注册", "/api/v1/user/register");
        paths.put("验证码", "/api/v1/user/notify");
        paths.put("健康检查", "/api/v1/health");
        paths.put("测试接口", "/api/v1/test/hello");
        
        log.info("路径测试接口被调用");
        return JsonData.buildSuccess(paths);
    }

    @PostMapping("/echo")
    public JsonData echo(@RequestBody Map<String, Object> data) {
        log.info("Echo接口被调用，接收到数据: {}", data);
        Map<String, Object> result = new HashMap<>();
        result.put("received", data);
        result.put("timestamp", System.currentTimeMillis());
        result.put("message", "数据回显成功");
        
        return JsonData.buildSuccess(result);
    }

    @GetMapping("/status")
    public JsonData getStatus() {
        Map<String, Object> status = new HashMap<>();
        status.put("service", "teach-platform");
        status.put("status", "running");
        status.put("timestamp", System.currentTimeMillis());
        
        return JsonData.buildSuccess(status);
    }

    @GetMapping("/error-test")
    public JsonData testError() {
        throw new RuntimeException("这是一个测试异常");
    }

    @GetMapping("/ping")
    public String ping() {
        log.info("Test ping endpoint called");
        return "pong";
    }

    @PostMapping("/login")
    public String testLogin(@RequestBody String body) {
        log.info("Test login endpoint called with body: {}", body);
        return "{\"status\":\"success\",\"message\":\"Test login endpoint working\"}";
    }
    
    @PostMapping("/jwt")
    public String testJWT(@RequestBody String tokenJson, HttpServletRequest request) {
        log.info("Test JWT endpoint called");
        
        try {
            // 从请求中获取token
            String token = JwtUtil.getTokenFromRequest(request);
            log.info("Token from request: {}", token);
            
            if (token != null) {
                // 验证token
                Claims claims = JwtUtil.checkJWT(token);
                log.info("Claims: {}", claims);
                
                if (claims != null) {
                    // 解析用户信息
                    User user = JwtUtil.parseUserFromClaims(claims);
                    log.info("Parsed user: {}", user);
                    
                    if (user != null) {
                        return "{\"status\":\"success\",\"message\":\"JWT parsing successful\",\"user\":{\"id\":" + user.getId() + ",\"name\":\"" + user.getName() + "\"}}";
                    } else {
                        return "{\"status\":\"error\",\"message\":\"User parsing failed\"}";
                    }
                } else {
                    return "{\"status\":\"error\",\"message\":\"Invalid token\"}";
                }
            } else {
                return "{\"status\":\"error\",\"message\":\"No token found\"}";
            }
        } catch (Exception e) {
            log.error("JWT test error: {}", e.getMessage(), e);
            return "{\"status\":\"error\",\"message\":\"" + e.getMessage() + "\"}";
        }
    }
} 